package net.ojbk.study.curriculumDesign.service.cilentservicer.impl;

import net.ojbk.study.curriculumDesign.beans.cilentservicer.Chat;
import net.ojbk.study.curriculumDesign.beans.cilentservicer.ChatMessage;
import net.ojbk.study.curriculumDesign.beans.cilentservicer.ChatSystem;
import net.ojbk.study.curriculumDesign.beans.cilentservicer.ServiceRecord;
import net.ojbk.study.curriculumDesign.beans.usermanage.User;
import net.ojbk.study.curriculumDesign.dao.cilentservicer.ServiceRecordDao;
import net.ojbk.study.curriculumDesign.dao.cilentservicer.ServicerDao;
import net.ojbk.study.curriculumDesign.service.cilentservicer.ICustomerCommService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/1/8
 * Time: 23:21
 * Description: No Description
 */
@Service("customerCommService")
public class CustomerCommServiceImpl implements ICustomerCommService {
    @Autowired
    @Qualifier("chatSystem")
    ChatSystem chatSystem;

    @Autowired
    @Qualifier("servicerDao")
    ServicerDao servicerDao;

    @Autowired
    @Qualifier("serviceRecordDao")
    ServiceRecordDao serviceRecordDao;

    @Override
    public Chat requestService(User user) throws Exception {
        System.out.println("客户端的ChatSystem地址：" + chatSystem);
        if (user == null || user.getUid() == null)
            throw new Exception("未登陆。");
        // 假设她之前没有会话
        Chat newChat = Chat.createNewChat(user);
        // 放入 chatBeginList内
        chatSystem.addNewBeginChat(newChat);
        return newChat;
    }

    /**
     * 获取所有聊天记录
     *
     * @param chat 聊天会话对象
     */
    @Override
    public List<ChatMessage> getMsgList(Chat chat) throws Exception {
        if (chat == null || chat.getMsgList() == null)
            throw new Exception("你确定这个会话存在？？？");
        return chat.getMsgList();
    }

    /**
     * 获取未读聊天记录
     *
     * @param chat 聊天会话对象
     * @return 获取New消息队列中的消息返回后，重新 NEW 一个空队列即可！！
     */
    @Override
    public ChatMessage getNewMessage(Chat chat) throws Exception {
        if (chat == null)
            throw new Exception("假会话？？");
        return chat.getCustomerNewMsgList();
    }

    /**
     * 发送一条消息给客服
     *
     * @param chat
     * @param newMsg
     */
    @Override
    public void sendMessage(Chat chat, ChatMessage newMsg) throws Exception {
        if (chat == null)
            throw new Exception("会话不存在");

        chat.sendMsgToSer(newMsg);
    }

    /**
     * 断开连接，还未评论。
     *
     * @param chat
     */
    @Override
    public void catService(Chat chat) throws Exception {
        if (chat == null)
            throw new Exception("会话不存在。");
        // 客服中心将此会话移除 chatingList,并设置chat状态,并将客服对象的服务队列删除该chat
        chatSystem.catChat(chat);
    }

    /**
     * 用户进行评分，并结束掉此会话！！
     *
     * @param chat
     * @param score
     */
    @Override
    public void endChat(Chat chat, double score) throws Exception {
        // 更新 客服的评分
        chat.endChat(score);
        System.out.println(chat.getServicer());
        servicerDao.updateOne(chat.getServicer());
        // 获得 就绪此次会话的 账单（ServiceRecord），并纯如数据库
        ServiceRecord serviceRecord = chat.getBill(score);
        // 存入数据库
        serviceRecordDao.insertOne(serviceRecord);
    }
}
